<template>
  <div class="app-container">
    <el-card class="box-card">
      <!--搜索-->
      <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="公告标题" prop="noticeTitle">
          <el-input
            v-model="queryParams.noticeTitle"
            placeholder="请输入公告标题"
            clearable=""
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="操作人员" prop="createBy">
          <el-input
            v-model="queryParams.createBy"
            placeholder="请输入操作人员"
            clearable=""
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="类型" prop="noticeType">
          <el-select v-model="queryParams.type" placeholder="公告类型" clearable size="small" value="">
            <el-option
              v-for="(item, index) in typeOptions" :key="index" :label="item.label"
              :value="item.value" :disabled="item.disabled"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>

      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
          >新增
          </el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="danger"
            plain
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDelete"
          >批删
          </el-button>
        </el-col>
      </el-row>

      <!--表格区域-->
      <el-table v-loading="loading" :data="noticeList" @selection-change="handleSelectionChange">
        <el-table-column type="expand" width="20">
          <template slot-scope="props">
            <el-form label-position="left" inline class="demo-table-expand">
              <el-col :span="24">
                <el-form-item style="width: 20%" label="创建人">
                  <span>{{ props.row.createBy }}</span>
                </el-form-item>
                <el-form-item style="width: 20%" label="创建时间">
                  <span>{{ props.row.createTime }}</span>
                </el-form-item>
                <el-form-item style="width: 20%" label="修改人">
                  <span>{{ props.row.updateBy }}</span>
                </el-form-item>
                <el-form-item style="width: 20%" label="修改时间">
                  <span>{{ props.row.modifiedTime }}</span>
                </el-form-item>
                <el-form-item style="width: 100%" label="通知标题">
                  <span>{{ props.row.noticeTitle }}</span>
                </el-form-item>
                <el-form-item style="width: 100%" label="通知内容：">
                  <div v-html="props.row.noticeContent"></div>
                </el-form-item>
              </el-col>
            </el-form>
          </template>
        </el-table-column>
        <el-table-column type="selection" width="50" align="center"/>
        <el-table-column label="序号" align="center" prop="noticeId" width="50"/>
        <el-table-column
          label="公告标题"
          align="center"
          prop="noticeTitle"
          width="300"
          :show-overflow-tooltip="true"/>
        <el-table-column
          label="公告类型"
          align="center"
          prop="noticeType"
          width="100">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.noticeType==='1'" size="mini">
              通知
            </el-tag>
            <el-tag v-else size="mini" type="success">
              公告
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="创建者" align="center" prop="createBy" width="100"/>
        <el-table-column label="创建时间" align="center" prop="createTime" width="170"></el-table-column>
        <el-table-column label="修改者" align="center" prop="updateBy" width="100"/>
        <el-table-column label="修改时间" align="center" prop="modifiedTime" width="170"></el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding " fixed="right" width="180">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
            >修改
            </el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
            >删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <!--分页-->
      <el-pagination
        style="padding-top: 15px"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="current"
        :page-sizes="[6, 10, 20, 30]"
        :page-size="size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>

      <!-- 添加或修改公告对话框 -->
      <el-dialog :title="title" :modal-append-to-body="false" :visible.sync="open" width="780px" append-to-body="">
        <el-form ref="form" :model="form" :rules="rules" label-width="80px">
          <el-row>
            <el-col :span="12">
              <el-form-item label="公告标题" prop="noticeTitle">
                <el-input v-model="form.noticeTitle" placeholder="请输入公告标题"/>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="公告类型" prop="noticeType">
                <el-select v-model="form.noticeType" placeholder="请选择" value="">
                  <el-option
                    v-for="(item, index) in typeOptions" :key="index" :label="item.label"
                    :value="item.value" :disabled="item.disabled"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="内容">
                <editor v-model="form.noticeContent" :min-height="192"/>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
  import {
    listNotice,
    delNotice,
    addNotice,
    updateNotice,
  } from '../../../api/system/notices'
  import Editor from '../../../components/Editor/index'

  export default {
    name: 'Notice',
    components: {
      Editor
    },
    data () {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 100,
        size: 6,
        current: 1,
        // 公告表格数据
        noticeList: [],
        // 弹出层标题
        title: '',
        // 是否显示弹出层
        open: false,
        // 状态数据字典
        typeOptions: [{
          'label': '通知',
          'value': '1'
        }, {
          'label': '公告',
          'value': '2'
        }],
        // 查询参数
        queryParams: {
          noticeTitle: undefined,
          createBy: undefined,
          type: undefined
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          noticeTitle: [
            {
              required: true,
              message: '公告标题不能为空',
              trigger: 'blur'
            }
          ],
          noticeType: [
            {
              required: true,
              message: '公告类型不能为空',
              trigger: 'change'
            }
          ]
        }
      }
    },
    created () {
      this.getList()
    },
    methods: {
      /** 查询公告列表 */
      getList () {
        this.loading = true
        listNotice(this.current, this.size, this.queryParams).then(res => {
          if (res) {
            this.noticeList = res.data.data.records
            this.total = res.data.data.total
            this.loading = false
          }
        })
      },
      // 取消按钮
      cancel () {
        this.open = false
        this.reset()
      },
      // 表单重置
      reset () {
        this.form = {
          noticeId: undefined,
          noticeTitle: undefined,
          noticeType: undefined,
          noticeContent: undefined,
          status: '0'
        }
        this.resetForm('form')
      },
      /** 搜索按钮操作 */
      handleQuery () {
        this.current = 1
        this.getList()
      },
      /** 重置按钮操作 */
      resetQuery () {
        this.resetForm('queryForm')
        this.handleQuery()
      },
      // 多选框选中数据
      handleSelectionChange (selection) {
        this.ids = selection.map(item => item.noticeId).join()
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd () {
        this.reset()
        this.open = true
        this.title = '添加公告'
      },
      /** 修改按钮操作 */
      handleUpdate (row) {
        this.reset()
        this.title = '修改公告'
        this.form = JSON.parse(JSON.stringify(row))
        this.open = true
      },
      /** 提交按钮 */
      submitForm: function () {
        this.$refs['form'].validate(valid => {
          if (valid) {
            if (this.form.noticeId !== undefined) {
              this.form.modifiedTime=undefined
              updateNotice(this.form).then(response => {
                if (response.data.code === 200) {
                  this.msgSuccess('修改成功')
                  this.open = false
                  this.getList()
                }
              })
            } else {
              addNotice(this.form).then(response => {
                if (response.data.code === 200) {
                  this.msgSuccess('新增成功')
                  this.open = false
                  this.getList()
                }
              })
            }
          }
        })
      },
      /** 删除按钮操作 */
      handleDelete (row) {
        const noticeIds = row.noticeId || this.ids
        this.$confirm('是否确认删除公告编号为"' + noticeIds + '"的数据项?', '警告', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(function () {
          delNotice(noticeIds).then(res => {
            if (res.data.code === 200) {
              this.getList()
              this.msgSuccess('删除成功')
            }
          })
        })
      },
      //当每页条数改变
      handleSizeChange (val) {
        this.size = val
        this.getList()
      },

      //当前页码改变
      handleCurrentChange (val) {
        this.current = val
        this.getList()
      },
    }
  }
</script>

<style lang="less" scoped>
  /deep/ p {
    width: 500px;
    display: block;
    margin-block-start: 0;
    margin-block-end: 0;
    margin-inline-start: 0;
    margin-inline-end: 0;
  }

  /deep/ .demo-table-expand {
    font-size: 0;
  }

  /deep/ .demo-table-expand label {
    line-height: normal;
    color: #99a9bf;
  }

  /deep/ .demo-table-expand {
    margin-right: 0;
    margin-bottom: 0;
    width: 100%;
  }

  /deep/ .el-form-item__content {
    line-height: normal;
    position: relative;
    font-size: 14px;
  }
</style>
